package com.team.game.Game.Towers;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.RectF;

import com.team.game.framework.Abstract.Tower;
import com.team.game.framework.Game;
import com.team.game.framework.impl.MainActivity;
import com.team.game.framework.impl.Scale_X_Y;
import com.team.game.framework.impl.SurfaceThread;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import static com.team.game.framework.impl.Scale_X_Y.scale;

public class TowerRange extends Tower {

    private int Attack_Counter = 0;
    private ArrayList<Bitmap> Attack_A = new java.util.ArrayList<>(6);
    private ArrayList<Bitmap> Boom_A = new ArrayList<>(6);

    TowerRange (RectF pos, Game game) {
        super (pos, game);


        recharge = 1;
        kick = 35;
        radius = (int) (800 * scale);
        radius1 = (int) (400 * scale);
        speed = 55 * scale;


        InputStream inputStream = null;
        try {
            inputStream = MainActivity.manager.open ("Picture/boom.png");
            boom = BitmapFactory.decodeStream (inputStream);

            inputStream = MainActivity.manager.open ("Picture/Towers/test_tower_2.png");
            tower = BitmapFactory.decodeStream (inputStream);

            inputStream = MainActivity.manager.open("Picture/Towers/RangeAttack/Range_Attack_1.png");
            Attack_A.add(BitmapFactory.decodeStream(inputStream));
            inputStream = MainActivity.manager.open("Picture/Towers/RangeAttack/Range_Attack_2.png");
            Attack_A.add(BitmapFactory.decodeStream(inputStream));
            inputStream = MainActivity.manager.open("Picture/Towers/RangeAttack/Range_Attack_3.png");
            Attack_A.add(BitmapFactory.decodeStream(inputStream));
            inputStream = MainActivity.manager.open("Picture/Towers/RangeAttack/Range_Attack_4.png");
            Attack_A.add(BitmapFactory.decodeStream(inputStream));
            inputStream = MainActivity.manager.open("Picture/Towers/RangeAttack/Range_Attack_5.png");
            Attack_A.add(BitmapFactory.decodeStream(inputStream));
            inputStream = MainActivity.manager.open("Picture/Towers/RangeAttack/Range_Attack_6.png");
            Attack_A.add(BitmapFactory.decodeStream(inputStream));


            inputStream = MainActivity.manager.open("Picture/Towers/RangeAttack/Range_Boom_1.png");
            Boom_A.add(BitmapFactory.decodeStream(inputStream));
            inputStream = MainActivity.manager.open("Picture/Towers/RangeAttack/Range_Boom_2.png");
            Boom_A.add(BitmapFactory.decodeStream(inputStream));
            inputStream = MainActivity.manager.open("Picture/Towers/RangeAttack/Range_Boom_3.png");
            Boom_A.add(BitmapFactory.decodeStream(inputStream));
            inputStream = MainActivity.manager.open("Picture/Towers/RangeAttack/Range_Boom_4.png");
            Boom_A.add(BitmapFactory.decodeStream(inputStream));
            inputStream = MainActivity.manager.open("Picture/Towers/RangeAttack/Range_Boom_5.png");
            Boom_A.add(BitmapFactory.decodeStream(inputStream));
            inputStream = MainActivity.manager.open("Picture/Towers/RangeAttack/Range_Boom_6.png");
            Boom_A.add(BitmapFactory.decodeStream(inputStream));

        } catch (IOException e) {
            e.printStackTrace ();
        }
    }

    @Override
    public void search (Canvas canvas) {

    }

    @Override
    public void detected (Canvas canvas) {

        canvas.drawBitmap(Attack_A.get(Attack_Counter), matrix, null);

    }

    @Override
    public void boom (Canvas canvas) {
        canvas.drawBitmap(Boom_A.get(Attack_Counter), matrix, null);
    }

    @Override
    public void drawTower (Canvas canvas) {
        if (boolMagAnimation) //активується коли башня знаходить моба
        {
            if (time > 0.1) {
                time = 0;
                if (magAnimation == 3) {
                    boolMagAnimation = false;
                    magAnimation = 0;
                    TowerSrc.left = 0;
                    TowerSrc.right = 48;
                } else {
                    magAnimation++;// робимо анімацію моба
                    TowerSrc.left += 48;
                    TowerSrc.right += 48;
                }
            } else
                time += SurfaceThread.deltaTime;

        }
        canvas.drawBitmap (tower, TowerSrc, position, null);
    }

    @Override
    public void towerRestart() {
        search = true;
        setAttack();
    }

    @Override
    public void dispose() {

    }

    @Override
    public boolean rectCompare(RectF rectF) {
        float x2, y2, x1, y1, x3, y3;
        double distance;
        x1 = rectAttack.centerX();
        y1 = rectAttack.centerY();
        x2 = rectF.centerX();
        y2 = rectF.centerY();

        float difX = x2 - x1;
        float difY = y2 - y1;

        distance = Math.sqrt(Math.pow((difX), 2.0) + Math.pow(difY, 2.0));
        float scale;
        if ((scale = (float) distance / speed * (float) (187.5 * SurfaceThread.deltaTime)) > 1) {
//             a=  (float)Math.sin((Math.sqrt (Math.pow (difY, 2.0)))/distance);
//             a = (float)Math.toDegrees(Math.asin(a));

            a = (float) ((Math.asin(difX / distance)) / (Math.PI)) * 180;

            x3 = ((difX) / scale);
            y3 = ((difY) / scale); //разберись с нужными/ненужными ректами/переменными

            rectAttack.right += x3;
            rectAttack.left += x3;
            rectAttack.bottom += y3;
            rectAttack.top += y3;

            //если делать только одно преобразование - нужно использовать сет
            //если их несколько - пре и пост
            //на форумах пишут что лучше пост
            matrix.reset();
            matrix.postScale(Scale_X_Y.Scale_X, Scale_X_Y.Scale_Y);
            matrix.postRotate(a, Attack_A.get(0).getWidth() / 2, Attack_A.get(0).getWidth() / 2);
            matrix.postTranslate(rectAttack.left, rectAttack.top);


            Attack_Counter++;
            if (Attack_Counter > 5)
                Attack_Counter = 0;
            //Log.d (TAG, "x: " + String.valueOf (x3) + " y:" + String.valueOf (y3));
            return false;
        } else {

            Attack_Counter = 0;
            matrix.reset();
            matrix.postScale(Scale_X_Y.Scale_X, Scale_X_Y.Scale_Y);
            matrix.postRotate(a, Boom_A.get(0).getWidth() / 2, Boom_A.get(0).getHeight() / 2);
            matrix.postTranslate(rectF.left, rectF.top);

            setAttack();

            detected = true;
            return true;
        }
    }


    @Override
    public void boom () {
        if (time > 0.03) {
            time = 0;
            if (Attack_Counter == 5) {
                Attack_Counter = 0;
                time_recharge = System.nanoTime() - time_recharge;
                search = true;
                detected = false;
            } else {
                Attack_Counter++;
            }
        } else
            time += SurfaceThread.deltaTime;
    }

}